home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATETIME.SWG / 0022_Day of the Week.pas < prev    next >
Pascal/Delphi Source File  |  1993-06-22  |  2KB  |  88 lines

  1. ===========================================================================
  2.  BBS: The Beta Connection
  3. Date: 06-07-93 (00:10)             Number: 773
  4. From: CYRUS PATEL                  Refer#: 744
  5.   To: STEPHEN WHITIS                Recvd: NO  
  6. Subj: DATE CALCULATIONS              Conf: (232) T_Pascal_R
  7. ---------------------------------------------------------------------------
  8. SW>Does anyone know where I can find an algorithm, or better yet TP
  9. SW>5.5 code, to calculate the day of the week for a give date?
  10.  
  11. Here's TP source for day of the week...
  12.  
  13. Const
  14.    CurrentYear = 1900;
  15.  
  16. Type
  17.    DateStr: String[8];
  18.  
  19.  
  20. Procedure ConvDate(DateStr: DateRecord;
  21.                    Var Month, Day, Year: Word);
  22.  
  23.  {this converts the date from string to numbers for month, day, and year}
  24.  
  25.   Var
  26.     ErrorCode: Integer;
  27.  
  28.   Begin
  29.     Val(Copy(DateStr, 1, 2), Month, ErrorCode);
  30.     Val(Copy(DateStr, 4, 2), Day, ErrorCode);
  31.     Val(Copy(DateStr, 7, 2), Year, ErrorCode);
  32.     Year := Year + CurrentYear
  33.   End;
  34.  
  35.  
  36. Function Dow(DateStr: DateRecord): Byte;
  37.  
  38.    {this returns the Day Of the Week as follows:
  39.          Sunday is 1, Monday is 2, etc...  Saturday is 7}
  40.  
  41.   Var
  42.     Month, Day, Year, Y1, Y2: Word;
  43.  
  44.   Begin
  45.     ConvDate(DateStr, Month, Day, Year);
  46.     If Month < 3 then
  47.       Begin
  48.       Month := Month + 10;
  49.       Year := Year - 1
  50.       End
  51.     else
  52.       Month := Month - 2;
  53.     Y1 := Year Div 100;
  54.     Y2 := Year Mod 100;
  55.     Dow := ((Day + Trunc(2.6 * Month - 0.1) + Y2 + Y2 Div 4 + Y1 Div 4 - 2 *
  56.            Y1 + 49) Mod 7) + 1
  57.   End;
  58.  
  59.  
  60. Here's an example of how to use it...
  61.  
  62. Begin
  63.    Case Dow('06/06/93') of
  64.      1: Write('Sun');
  65.      2: Write('Mon');
  66.      3: Write('Tues');
  67.      4: Write('Wednes');
  68.      5: Write('Thurs');
  69.      6: Write('Fri');
  70.      7: Write('Satur')
  71.    End;
  72.    WriteLn('day')
  73. End.
  74.  
  75.  
  76. SW>And I just know I've run across an algorithm or code to do this
  77. SW>before, but it was a while back, and I've looked in the places I
  78. SW>thought it might have been.  Any ideas?
  79.  
  80. You might want to take a look at Dr. Dobbs from a few months back
  81. (earlier this year), they had an whole issue related to dates
  82.  
  83. Cyrus
  84. ---
  85.  ■ SPEED 1·30 #666 ■ 2!  4!  6!  8!  It's time to calculate!  2 24 720 40,32
  86.  * Midas Touch of Chicago 312-764-0591/0761 DUAL STD
  87.  * PostLink(tm) v1.06  MIDAS (#887) : RelayNet(tm) Hub
  88.